import { allCity, province } from './data';

import type { CityValue } from './index';

// 获取省份名称
export const getProvinceLabel = (provinceValue: string): string => {
  const provinceItem = province.find(p => p.value === provinceValue);
  return provinceItem?.label || provinceValue;
};

// 获取城市名称
export const getCityLabel = (cityValue: string): string => {
  const cityItem = allCity.find(c => c.label === cityValue);
  return cityItem?.label || cityValue;
};

// 获取完整地址
export const getFullAddress = (value: CityValue): string => {
  if (!value?.province) return '';
  const provinceName = getProvinceLabel(value.province);
  const cityName = value.city ? getCityLabel(value.city) : '';
  return cityName ? `${provinceName} ${cityName}` : provinceName;
};

// 验证省份是否存在
export const isValidProvince = (provinceValue: string): boolean => {
  return province.some(p => p.value === provinceValue);
};

// 验证城市是否属于指定省份
export const isValidCity = (cityValue: string, provinceValue: string): boolean => {
  return allCity.some(c => c.label === cityValue && c.prov === provinceValue);
};

// 获取省份下的所有城市
export const getCitiesByProvince = (provinceValue: string) => {
  return allCity
    .filter(city => city.prov === provinceValue)
    .map(city => ({
      label: city.label,
      value: city.label,
    }));
};
